package com.jd.finance.core.controller.admin;


import com.jd.finance.core.service.*;
import com.jd.finance.core.service.impl.AdminUtilsServiceImpl;
import com.jd.finance.result.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 贷款协议表 前端控制器
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-26
 */
@Api(tags = "HR中心")
@Slf4j
@RestController
@RequestMapping("admin/core/adminHrCenter")
public class AdminHrController {
    @Resource
    private RoleService roleService;

    @Resource
    private AssignService assignService;

    @Resource
    private AdminUtilsServiceImpl adminUtilsServiceimpl;

    @Resource
    private NodeService nodeService;

    @Resource
    private GrantService grantService;

    @Resource
    private AdminUtilsServiceImpl adminUtilsService;

    @Resource
    private ConnectService connectService;

    @Resource
    private OrganizationService organizationService;

    @Resource
    private UserService userService;

    @ApiOperation("查看所有角色信息")
    @GetMapping("/selectAllRoleInfo")
    public BaseResult selectAllRoleInfo(Integer uid){
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isHR(uid)){
            log.error("当前登录用户不是hr账户");
            return BaseResult.error().message("当前登录用户不是HR账户");
        }
        return roleService.selectAllRoleInfo(uid);
    }

    @ApiOperation("添加rbac中间表")
    @PostMapping("/addUserRole")
    public BaseResult addUserRole(Integer uid, Integer uid1, Integer roleId, HttpServletRequest request){
        if(uid == null){
            log.error("管理员id为空");
            return BaseResult.error().message("管理员id为空");
        }
        if(uid1 == null){
            log.error("用户id为空");
            return BaseResult.error().message("用户id为空");
        }
        if(roleId == null){
            log.error("角色id为空");
            return BaseResult.error().message("角色id为空");
        }
        if (!adminUtilsServiceimpl.isHR(uid)){
            log.error("当前登录用户不是hr账户");
            return BaseResult.error().message("当前登录用户不是HR账户");
        }
        return assignService.addUserRole(uid,uid1,roleId,request);
    }

    @ApiOperation("添加角色信息")
    @PostMapping("/addRoleInfo")
    public BaseResult addRoleInfo(Integer uid,String roleName,String roleExplain,String roleOrgan,HttpServletRequest request){
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isHR(uid)){
            log.error("当前登录用户不是hr账户");
            return BaseResult.error().message("当前登录用户不是HR账户");
        }
        return roleService.addRoleInfo(uid,roleName,roleExplain,roleOrgan,request);
    }

    @ApiOperation("查看所有节点信息")
    @GetMapping("/selectNodeInfo")
    public BaseResult selectNodeInfo(Integer uid){
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isHR(uid)){
            log.error("当前登录用户不是hr账户");
            return BaseResult.error().message("当前登录用户不是HR账户");
        }
        return nodeService.selectNodeInfo(uid);
    }

    @ApiOperation("添加角色-节点中间表")
    @PostMapping("/addNodeRole")
    public BaseResult addNodeRole(Integer uid,Integer roleId, Integer listId,  HttpServletRequest request){
        if(uid == null){
            log.error("管理员id为空");
            return BaseResult.error().message("管理员id为空");
        }
        if(listId == null){
            log.error("节点id为空");
            return BaseResult.error().message("节点id为空");
        }
        if(roleId == null){
            log.error("角色id为空");
            return BaseResult.error().message("角色id为空");
        }
        if (!adminUtilsServiceimpl.isHR(uid)){
            log.error("当前登录用户不是hr账户");
            return BaseResult.error().message("当前登录用户不是HR账户");
        }
        return grantService.addNodeRole(uid,listId,roleId,request);
    }

    @ApiOperation("添加节点（节点名、url）")
    @PostMapping("/addNode")
    public BaseResult addNode(Integer uid, String listName, String listUrl,HttpServletRequest request){
        if (uid == null) {
            log.error("参数为空S");
            return BaseResult.error().message("参数为空S");
        }
        if (StringUtils.isAllBlank(listName, listUrl)) {
            log.error("参数为空S");
            return BaseResult.error().message("参数为空S");
        }
        if (!adminUtilsService.isHR(uid)) {
            log.error("无访问权限S");
            return BaseResult.error().message("无访问权限");
        }

        return  nodeService.addNode(uid,listName, listUrl, request);
    }

    @ApiOperation("发起交接申请")
    @PostMapping("/applyConn")
    public BaseResult applyConn(Integer uid, Integer leaveId, Integer onId, String loanApplyId){
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if(leaveId == null){
            log.error("离职人员id为空");
            return BaseResult.error().message("离职人员id为空");
        }
        if(onId == null){
            log.error("在职人员id为空");
            return BaseResult.error().message("在职人员id为空");
        }
        if(loanApplyId == null){
            log.error("负责的业务id为空");
            return BaseResult.error().message("负责的业务id为空");
        }
        if (!adminUtilsServiceimpl.isHR(uid)){
            log.error("当前登录用户不是hr账户");
            return BaseResult.error().message("当前登录用户不是HR账户");
        }
        return connectService.applyConn(uid,leaveId,onId,loanApplyId);
    }

    @ApiOperation("审核交接申请")
    @PostMapping("/examConnectApply")
    public BaseResult examConnectApply(Integer uid,Integer connectId,Integer isPass){
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isGM(uid)){
            log.error("当前登录用户不是总经理账户");
            return BaseResult.error().message("当前登录用户不是总经理账户");
        }
        return connectService.examConnectApply(uid,connectId,isPass);
    }

    @ApiOperation("业务交接")
    @PostMapping("/examConnect")
    public BaseResult examConnect(Integer uid, Integer leaveId, Integer onId, String loanApplyId){
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if(leaveId == null){
            log.error("离职人员id为空");
            return BaseResult.error().message("离职人员id为空");
        }
        if(onId == null){
            log.error("在职人员id为空");
            return BaseResult.error().message("在职人员id为空");
        }
        if(loanApplyId == null){
            log.error("负责的业务id为空");
            return BaseResult.error().message("负责的业务id为空");
        }
        if (!adminUtilsServiceimpl.isHR(uid)){
            log.error("当前登录用户不是hr账户");
            return BaseResult.error().message("当前登录用户不是HR账户");
        }
        return connectService.examConnect(uid,leaveId,onId,loanApplyId);
    }


    @ApiOperation("查询所有部门名称")
    @GetMapping("/getAllDepartment")
    public BaseResult getAllDepartment(Integer uid) {
        if (uid==null) {
            log.error("该用户非HR身份!");
            return BaseResult.error().message("该用户非HR身份!");
        }

        return organizationService.getAllDepartment(uid);

    }

    @ApiOperation("根据uid删除RBAC中间表")
    @GetMapping("/deleteUserRole")
    public BaseResult deleteUserRole(Integer uid,Integer leaveId,HttpServletRequest request){
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if(leaveId == null){
            log.error("离职人员id为空");
            return BaseResult.error().message("离职人员id为空");
        }
        if (!adminUtilsServiceimpl.isHR(uid)){
            log.error("当前登录用户不是hr账户");
            return BaseResult.error().message("当前登录用户不是hr账户");
        }
        return assignService.deleteUserRole(uid,leaveId,request);
    }

    @ApiOperation("根据uid删除用户表")
    @GetMapping("/deleteUserTableById")
    public BaseResult deleteUserTableById(Integer uid,Integer leaveId,HttpServletRequest request){
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if(leaveId == null){
            log.error("离职人员id为空");
            return BaseResult.error().message("离职人员id为空");
        }
        if (!adminUtilsServiceimpl.isHR(uid)){
            log.error("当前登录用户不是hr账户");
            return BaseResult.error().message("当前登录用户不是hr账户");
        }
        return userService.deleteUserTableById(uid,leaveId,request);
    }

    @ApiOperation("查询所有后台用户信息")
    @GetMapping("/getAllAdminInfo")
    public BaseResult getAllAdminInfo(Integer uid){
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isHR(uid)){
            log.error("当前登录用户不是hr账户");
            return BaseResult.error().message("当前登录用户不是hr账户");
        }
        return userService.getAllAdminInfo(uid);
    }

    @ApiOperation("根据手机号或者姓名查询后台用户信息")
    @GetMapping("/getAdminAccountInfo")
    public BaseResult getAdminAccountInfo(Integer uid,String userInfo){
        if(uid == null){
            log.error("uid为空");
            return BaseResult.error().message("uid为空");
        }
        if (!adminUtilsServiceimpl.isHR(uid)){
            log.error("当前登录用户不是hr账户");
            return BaseResult.error().message("当前登录用户不是hr账户");
        }
        return userService.getAdminAccountInfo(uid,userInfo);
    }
}

